package com.csweimei.rentmanagementtreasure.tenant.viewModel;

import android.app.Application;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.databinding.ObservableArrayList;
import androidx.databinding.ObservableField;
import androidx.databinding.ObservableList;

import com.chad.library.BR;
import com.csweimei.rentmanagementtreasure.R;
import com.csweimei.rentmanagementtreasure.base.AppConfig;
import com.csweimei.rentmanagementtreasure.http.entity.SearchEntBean;
import com.csweimei.rentmanagementtreasure.viewModel.TitleVM;

import java.util.Map;

import io.reactivex.observers.DisposableObserver;
import me.goldze.mvvmhabit.base.BaseModel;
import me.goldze.mvvmhabit.utils.RxUtils;
import me.tatarka.bindingcollectionadapter2.ItemBinding;

public class SearchTeamVM extends TitleVM {
    public SearchTeamVM(@NonNull Application application, BaseModel model) {
        super(application, model);
        setTitleText("加入团队");
    }

    public ObservableField<Integer> showNo = new ObservableField<>(View.VISIBLE);

    public ObservableList<SearchEntItemVM> observableList = new ObservableArrayList<>();
    public ItemBinding<SearchEntItemVM> itemBinding = ItemBinding.of(BR.viewModel, R.layout.item_team);
    public void searchEnt(String work){
        Map<String,String> map = AppConfig.getCurMap(model);
        map.put("companyname",work);
        model.seachcompany(map)
                .compose(RxUtils.schedulersTransformer())
                .compose(RxUtils.exceptionTransformer())
                .doOnSubscribe(this)
                .subscribe(new DisposableObserver<SearchEntBean>() {
                    @Override
                    public void onNext(SearchEntBean o) {
                        observableList.clear();
                        if (o.issucc){
                            if (o.getData().size()>0){
                                showNo.set(View.GONE);
                                for (SearchEntBean.DataDTO bean:o.getData()) {
                                    SearchEntItemVM searchEntItemVM = new SearchEntItemVM(SearchTeamVM.this);
                                    searchEntItemVM.setData(bean);
                                    observableList.add(searchEntItemVM);

                                }
                            }else {
                                showNo.set(View.VISIBLE);
                            }
                        }else {
                            showNo.set(View.VISIBLE);
                        }
                    }

                    @Override
                    public void onError(Throwable e) {
                        showNo.set(View.VISIBLE);
                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }
}
